package com.flying.flow.service;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.flow.model.Node;
import com.flying.flow.model.NodeTransition;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.exception.AppException;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;

@Service(value="NodeTransitionService", desc="")
public class NodeTransitionService extends AbstractService{
	private NodeService ns;
	
	public void setNodeService(NodeService s) {
		this.ns = s;
	}
    
	@MethodInfo("新增")
	public NodeTransition create(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id,
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点") Long to_node_id,
			@Param(value=NodeTransition.NAME, required=false,	desc="名称") String name,
			@Param(value=NodeTransition.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=NodeTransition.CONDITION, required=false,	desc="条件") String condition
		) throws Exception {
		Node fn = this.ns.findById(from_node_id);
		Node tn = this.ns.findById(to_node_id);
		if(fn.getFlowDefId() != tn.getFlowDefId()) {
			throw new AppException("0001", "不是同一流程定义中的两个节点不能连接！");
		}
		assert fn != null && tn != null && fn.getFlowDefId().longValue() == tn.getFlowDefId().longValue();
		return this.create2(fn.getFlowDefId(), name, remarks, from_node_id, to_node_id, condition);
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=NodeTransition.ENTITY_NAME)
	public NodeTransition create2(
			@Param(value=NodeTransition.FLOW_DEF_ID, required=true,	desc="流程定义ID") Long flow_def_id,
			@Param(value=NodeTransition.NAME, required=false,	desc="名称") String name,
			@Param(value=NodeTransition.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id,
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点") Long to_node_id,
			@Param(value=NodeTransition.CONDITION, required=false,	desc="条件") String condition
		) throws Exception {
		Node fn = this.ns.findById(from_node_id);
		Node tn = this.ns.findById(to_node_id);
		if(fn.getFlowDefId() != tn.getFlowDefId()) {
			throw new AppException("0001", "不是同一流程定义中的两个节点不能连接！");
		}
		return null;
	}
    
	@MethodInfo("新增")
	@DaoUpdate(entity=NodeTransition.ENTITY_NAME)
	public ActionResult update(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点", tag="=") Long from_node_id,
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点", tag="=") Long to_node_id,
			@Param(value=NodeTransition.NAME, required=false,	desc="名称") String name,
			@Param(value=NodeTransition.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=NodeTransition.CONDITION, required=false,	desc="条件") String condition
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=NodeTransition.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id,
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点") Long to_node_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=NodeTransition.ENTITY_NAME)
	public ActionResult removeByFlowDefinitionId(
			@Param(value=NodeTransition.FLOW_DEF_ID, required=true,	desc="流程定义ID") Long flow_def_id) {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=NodeTransition.ENTITY_NAME)
	public ActionResult removeByFromNodeId(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=NodeTransition.ENTITY_NAME)
	public ActionResult removeByToNodeId(
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点") Long to_node_id
		) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=NodeTransition.ENTITY_NAME, pageable=true, modelClass=NodeTransition.class)
	public QueryResult<NodeTransition> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=NodeTransition.ENTITY_NAME, pageable=false, modelClass=NodeTransition.class)
	public QueryResult<NodeTransition> findByFlowDefinitionId(
			@Param(value=NodeTransition.FLOW_DEF_ID, required=true,	desc="流程定义ID") Long flow_def_id) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=NodeTransition.ENTITY_NAME, pageable=false, modelClass=NodeTransition.class)
	public QueryResult<NodeTransition> findByFromNodeId(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=NodeTransition.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public NodeTransition findByTowNodeId(
			@Param(value=NodeTransition.FROM_NODE_ID, required=true,	desc="起始节点") Long from_node_id,
			@Param(value=NodeTransition.TO_NODE_ID, required=true,	desc="终止节点") Long to_node_id
		) {
		return null;
	}

}